const formatTime = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${[year, month, day].map(formatNumber).join("/")} ${[
    hour,
    minute,
    second,
  ]
    .map(formatNumber)
    .join(":")}`;
};

const formatNumber = (n) => {
  n = n.toString();
  return n[1] ? n : `0${n}`;
};

module.exports = {
  formatTime,
  /**
   * 格式化日期格式 (用于兼容ios Date对象)
   */
  format_date(time) {
    // 将xxxx-xx-xx的时间格式，转换为 xxxx/xx/xx的格式
    return time.replace(/\-/g, "/");
  },
  /**
   * 对象转URL
   */
  urlEncode(data) {
    var _result = [];

    for (var key in data) {
      var value = data[key];

      if (value.constructor == Array) {
        value.forEach((_value) => {
          _result.push(key + "=" + _value);
        });
      } else {
        _result.push(key + "=" + value);
      }
    }

    return _result.join("&");
  },

  /**
   * 遍历对象
   */
  objForEach(obj, callback) {
    Object.keys(obj).forEach((key) => {
      callback(obj[key], key);
    });
  },

  /**
   * 是否在数组内
   */
  inArray(search, array) {
    for (var i in array) {
      if (array[i] == search) {
        return true;
      }
    }

    return false;
  },

  /**
   * 判断是否为正整数
   */
  isPositiveInteger(value) {
    return /(^[0-9]\d*$)/.test(value);
  },
  /**
   * scene解码
   */
  scene_decode(e) {
    if (e === undefined) {
      return {};
    }

    let scene = decodeURIComponent(e);
    let params = scene.split(",");
    let data = {};
    for (let i in params) {
      var val = params[i].split(":");

      if (val.length > 0 && val[0]) {
        data[val[0]] = val[1] || null;
      }
    }

    return data;
  },
  /**
   * 动态引入js
   */
  importScript(url) {
    return new Promise((resolve, reject) => {
      const script = document.querySelector(`script[src="${url}"]`);
      if (script) {
        resolve();
        return;
      }
      const newScript = document.createElement("script");
      newScript.addEventListener("load", () => {
        resolve();
      });
      newScript.addEventListener("error", () => {
        reject(new Error(`Failed to load script ${url}`));
      });
      newScript.src = url;
      document.head.appendChild(newScript);
    });
  },
};
